package su.security.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import net.kuakao.core.dto.PagerDTO;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import su.base.Constants;
import su.base.dao.SuBaseDAO;
import su.security.dao.ISecurityModuleDAO;
import su.security.entity.SecurityModule;


@Repository
public class SecurityModuleDAO extends SuBaseDAO implements ISecurityModuleDAO {
	public List<SecurityModule> queryByPage(PagerDTO s)throws SQLException{
		List<SecurityModule> res = null;
		StringBuilder sql = new StringBuilder("select * from su_security_module where status=1");
		if(StringUtils.isNotBlank(s.getS1())){//id
			if(s.getS1().indexOf(",")>-1){
				sql.append(" and id in ("+s.getS1()+")");
			}else{
				sql.append(" and id="+s.getS1());
			}
			
		}
		if(StringUtils.isNotBlank(s.getS2()))sql.append(" and modulename like '%"+s.getS2()+"%'");
		sql.append(" ORDER BY id desc");
		
		return super.queryByPage(sql.toString(), new RowMapper<SecurityModule>() {

			public SecurityModule mapRow(ResultSet rs, int arg1) throws SQLException {
				SecurityModule entity = new SecurityModule();
				entity.setId(rs.getInt("id"));
				entity.setModuledesc(rs.getString("moduledesc"));
				entity.setModulename(rs.getString("modulename"));
				entity.setModuletype(rs.getInt("moduletype"));
				entity.setStatus(rs.getInt("status"));
				entity.setUrlids(rs.getString("urlids"));
				return entity;
			}
			
		}, s);
	}
	
	public SecurityModule getBaseinfo(int id)throws SQLException{
		String sql = "select * from su_security_module where status=1 and id=?";
		return super.queryForObject(sql,new Object[]{id}, new RowMapper<SecurityModule>() {

			public SecurityModule mapRow(ResultSet rs, int arg1) throws SQLException {
				SecurityModule entity = new SecurityModule();
				entity.setId(rs.getInt("id"));
				entity.setModuledesc(rs.getString("moduledesc"));
				entity.setModulename(rs.getString("modulename"));
				entity.setModuletype(rs.getInt("moduletype"));
				entity.setStatus(rs.getInt("status"));
				entity.setUrlids(rs.getString("urlids"));
				return entity;
			}
			
		});
	}
	
	public List<SecurityModule> getAllModuleList()throws SQLException{
		String sql = "select * from su_security_module where status=1 ORDER BY id DESC";
		return super.query(sql.toString(), new RowMapper<SecurityModule>() {

			public SecurityModule mapRow(ResultSet rs, int arg1) throws SQLException {
				SecurityModule entity = new SecurityModule();
				entity.setId(rs.getInt("id"));
				entity.setModuledesc(rs.getString("moduledesc"));
				entity.setModulename(rs.getString("modulename"));
				entity.setModuletype(rs.getInt("moduletype"));
				entity.setStatus(rs.getInt("status"));
				entity.setUrlids(rs.getString("urlids"));
				return entity;
			}
			
		});
	}
	
	public List<SecurityModule> getModuleList(String ids)throws SQLException{
		String sql = "select * from su_security_module where status=1 and id in ("+ids+") ORDER BY id";
		return super.query(sql.toString(), new RowMapper<SecurityModule>() {

			public SecurityModule mapRow(ResultSet rs, int arg1) throws SQLException {
				SecurityModule entity = new SecurityModule();
				entity.setId(rs.getInt("id"));
				entity.setModuledesc(rs.getString("moduledesc"));
				entity.setModulename(rs.getString("modulename"));
				entity.setModuletype(rs.getInt("moduletype"));
				entity.setStatus(rs.getInt("status"));
				entity.setUrlids(rs.getString("urlids"));
				return entity;
			}
			
		});
	}
	
	
	public void insert(SecurityModule o) throws SQLException{
		String sql = "insert into su_security_module(modulename,moduledesc,urlids,status,moduletype) values(?,?,?,?,?)";
		super.update(sql, new Object[]{o.getModulename(),o.getModuledesc(),o.getUrlids(),o.getStatus(),o.getModuletype()});
	}
	public void update(SecurityModule o) throws SQLException{
		String sql = "update su_security_module set modulename=?,moduledesc=?,urlids=?,status=?,moduletype=? where id=?";
		super.update(sql, new Object[]{o.getModulename(),o.getModuledesc(),o.getUrlids(),o.getStatus(),o.getModuletype(),o.getId()});
	}
	public void remove(int id) throws SQLException{
		String sql = "update su_security_module set status=? where id=?";
		super.update(sql, new Object[]{Constants.STATUS_DISABLE,id});
	}
}

